package cn.shlbysj.demo.service.impl;

import cn.shlbysj.demo.common.ConstansUtil;
import cn.shlbysj.demo.common.PageResult;
import cn.shlbysj.demo.common.Result;
import cn.shlbysj.demo.common.UuidUtil;
import cn.shlbysj.demo.entity.ArtInfo;
import cn.shlbysj.demo.entity.ArtInfoExample;
import cn.shlbysj.demo.mapper.ArtInfoMapper;
import cn.shlbysj.demo.service.IArticleService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("articleService")
@Transactional
public class IArticleServiceImpl implements IArticleService{
    @Autowired
    private ArtInfoMapper artInfoDao;

    @Override
    public PageResult findPage(String title,Integer type, int page, int rows) {
        //设置分页查询条件
        PageHelper.startPage(page,rows);
        ArtInfoExample example=new ArtInfoExample();
        example.setOrderByClause("`create_time` desc");
        ArtInfoExample.Criteria criteria = example.createCriteria();
        if(title!=null && !"".equals(title)){
            criteria.andArtNameLike(title);
        }
        if(type!=null && !"".equals(type.toString())){
            criteria.andTypeIdEqualTo(type);
        }
        Page<ArtInfo> pageList = (Page<ArtInfo>) artInfoDao.selectByExample(example);
        return new PageResult(pageList.getTotal(),pageList.getResult());
    }

    @Override
    public List<ArtInfo> getListFan() {
        PageHelper.startPage(1,8);
        ArtInfoExample example=new ArtInfoExample();
        example.setOrderByClause("`create_time` desc");
        ArtInfoExample.Criteria criteria = example.createCriteria();
        criteria.andIsHotEqualTo(ConstansUtil.ART_YES_REC);
        return artInfoDao.selectByExample(example);
    }

    @Override
    public List<ArtInfo> getListHot() {
        PageHelper.startPage(1,8);
        ArtInfoExample example=new ArtInfoExample();
        example.setOrderByClause("`create_time` desc");
        ArtInfoExample.Criteria criteria = example.createCriteria();
        criteria.andIsHotEqualTo(ConstansUtil.ART_YES_HOT);
        return artInfoDao.selectByExample(example);
    }

    @Override
    public Result insert(ArtInfo artInfo) {
        artInfo.setArtId(UuidUtil.getUUID());
        artInfo.setCreateTime(new Date());
        int ret=artInfoDao.insert(artInfo);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }

    @Override
    public Result update(ArtInfo artInfo) {
        int ret=artInfoDao.updateByPrimaryKeySelective(artInfo);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }
    @Override
    public Result delete(String artId) {
        int ret=artInfoDao.deleteByPrimaryKey(artId);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }

    @Override
    public List<ArtInfo> getListByType(Integer type) {
        Page<ArtInfo> list =null;
        PageHelper.startPage(1,8);
        ArtInfoExample example=new ArtInfoExample();
        example.setOrderByClause("`create_time` desc");
        ArtInfoExample.Criteria criteria = example.createCriteria();
        if(type==1){//最新设计
            list=(Page<ArtInfo>)artInfoDao.selectByExample(example);
        }else if (type==2){//精彩推荐
            criteria.andIsRecomEqualTo(1);
            list=(Page<ArtInfo>)artInfoDao.selectByExample(example);
        }else if (type==3){//热门
            criteria.andIsHotEqualTo(1);
            list=(Page<ArtInfo>)artInfoDao.selectByExample(example);
        }
        return list;
    }
}
